package com.yumei.trader.gym.util;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.ActivityManager;
import android.content.ComponentName;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.telephony.TelephonyManager;
import android.text.TextUtils;
import android.view.WindowManager;

public class DeviceUtil {

	/***
	 * 获取应用版本号
	 * @return 应用版本号
	 */
	public static int getVersionCode(Context c) {

		try {
			PackageManager packageManager = c.getPackageManager();
			PackageInfo packInfo = packageManager.getPackageInfo(c.getPackageName(), 0);
			return packInfo.versionCode;
			
		} catch (Exception e) {
			e.printStackTrace();
		}

		return 0;
	}

	/**
	 * 获取项目的渠道号
	 */
	public static String getChannel(Context context) {
		ApplicationInfo ai;
		try {
			ai = context.getPackageManager().getApplicationInfo(context.getPackageName(), PackageManager.GET_META_DATA);
			Bundle bundle = ai.metaData;
			return bundle.getString("APP_CHANNEL");
		} catch (PackageManager.NameNotFoundException e) {
			e.printStackTrace();
		}
		return "";
	}
	
	/**
	 * 获取应用版本名
	 * @return 应用版本名
	 */
	public static String getVersionName(Context c) {
		try {
			PackageManager packageManager = c.getPackageManager();
			PackageInfo packInfo = packageManager.getPackageInfo(c.getPackageName(), 0);
			return packInfo.versionName;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * 获取当前手机系统版本号
	 * @return  系统版本号
	 */
	public static String getSystemVersion() {
		return android.os.Build.VERSION.RELEASE;
	}

	/**
	 * 获取手机厂商
	 * @return  手机厂商
	 */
	public static String getDeviceBrand() {
		return android.os.Build.BRAND;
	}

	/**
	 * 获取手机型号
	 * @return  手机型号
	 */
	public static String getSystemModel() {
		return android.os.Build.MODEL;
	}
	
	/**
	 * 获取设备IMEI号
	 * @return 设备IMEI号
	 */
	@SuppressLint({"MissingPermission", "HardwareIds", "ApplySharedPref"})
	public static String getDeviceID(Context c) {
		
		SharedPreferences config= c.getSharedPreferences("APP_CONFIG", 0);
		String deviceID = config.getString("IMEI", null);
		
		if(deviceID == null){
		
			TelephonyManager telPhoneManage = (TelephonyManager)c.getSystemService(Context.TELEPHONY_SERVICE);
			
			deviceID = telPhoneManage.getDeviceId();

			if(deviceID == null || "".equals(deviceID)){
				deviceID = "R+"+String.valueOf(System.currentTimeMillis()); 
			}
			
			SharedPreferences.Editor editor = config.edit();
			editor.putString("APP_CONFIG", deviceID);
			editor.commit();
		}

		return deviceID;
	}
	
	/**
	 * 根据Wifi信息获取本地Mac
	 * @return 本地Mac
	 */
	public static String getDeviceMacAddressByWifi(Context context) {

	    @SuppressLint("WifiManagerPotentialLeak")
		WifiManager wifi = (WifiManager)context.getSystemService(Context.WIFI_SERVICE);
	    WifiInfo info = wifi.getConnectionInfo(); 
	    @SuppressLint("HardwareIds")
		String macAdress = info.getMacAddress(); //获取mac地址
	    
	    return macAdress;
	}
	
	
	
	/**
	 * 判断是否当前运行
	 * @return 结果
	 */
	public static boolean isRunningForeground (Context context){
		
	    ActivityManager am = (ActivityManager)context.getSystemService(Context.ACTIVITY_SERVICE);
	    ComponentName cn = am.getRunningTasks(1).get(0).topActivity;
	    String currentPackageName = cn.getPackageName();

		return !TextUtils.isEmpty(currentPackageName) && currentPackageName.equals(context.getPackageName());
	}



	/****
	 * 设置添加屏幕的背景透明度
	 */
	public static void setWindonwAlpha(Context context,float bgAlpha) {

		Activity act = (Activity)context;

		WindowManager.LayoutParams lp = act.getWindow().getAttributes();
		lp.alpha = bgAlpha; //0.0-1.0
		act.getWindow().setAttributes(lp);
		//act.getWindow().setFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND,
		//        WindowManager.LayoutParams.FLAG_DIM_BEHIND);
		// act.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
	}

}
